﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using Keepo.Models;
using Keepo.Helpers;
using AlbumDesigner.Models.Enums;
using System.IO;

namespace Keepo.DAL
{
    public class AlbumInitializer : CreateDatabaseIfNotExists<AlbumContext>
    {
        protected override void Seed(AlbumContext context)
        {
            context.Database.ExecuteSqlCommand("CREATE PROCEDURE SetAllXmlAsFalse AS BEGIN UPDATE AlbumPage SET HasXml='false' END");

            var userpermissions = new List<UserPermission>
            {
                new UserPermission {  UserPermissionId=1, UserPermissionName="לקוח" },
                new UserPermission {  UserPermissionId=2, UserPermissionName="מנהל" },
                new UserPermission {  UserPermissionId=3, UserPermissionName="איש סטודיו" },
                new UserPermission {  UserPermissionId=4, UserPermissionName="איש מכירות" },
                new UserPermission {  UserPermissionId=5, UserPermissionName="צלם" }
            };

            userpermissions.ForEach(s => context.UserPermissions.Add(s));
            context.SaveChanges();

            var users = new List<User>
            {
                new User { UserId=1, Username = "Asif",Email="asifycm@gmail.com",  Password="12345",  FirstName="Asif", LastName="Ytzhak", ActivationKey="dfdfg", Activate=true, RegistrationDate= DateTime.Now, UserPermissionId=2, NewsletterSubscription = false },
                new User { UserId=2, Username = "Elad",Email="e.aharoni@gmail.com", Password="12345", FirstName="Elad", LastName="Aharoni", ActivationKey="dfdfg", Activate=true, RegistrationDate= DateTime.Now, UserPermissionId=2, NewsletterSubscription = false },
                new User { UserId=3, Username = "Elad2",Email="e.aharoni@gmail.com", Password="12345", FirstName="Elad", LastName="Aharoni", ActivationKey="dfdfg", Activate=true, RegistrationDate= DateTime.Now, UserPermissionId=4, NewsletterSubscription = false },
                new User { UserId=4, Username = "Elad3",Email="e.aharoni@gmail.com", Password="12345", FirstName="Elad", LastName="Aharoni", ActivationKey="dfdfg", Activate=true, RegistrationDate= DateTime.Now, UserPermissionId=4, NewsletterSubscription = false },
                new User { UserId=5, Username = "Elad4",Email="e.aharoni@gmail.com", Password="12345", FirstName="Elad", LastName="Aharoni", ActivationKey="dfdfg", Activate=true, RegistrationDate= DateTime.Now, UserPermissionId=5, NewsletterSubscription = false }
            };

            users.ForEach(s => context.Users.Add(s));
            context.SaveChanges();

            var imageCategories = new List<ImageCategory>
            {
                new ImageCategory { Title = "Default", TitleHebrew = "כללי", Deletable = false }
            };

            imageCategories.ForEach(s => context.ImageCategories.Add(s));
            context.SaveChanges();

            var coverTypes = new List<CoverType>
            {
                new CoverType { Title = "Soft" },
                new CoverType { Title = "Hard" }
            };

            coverTypes.ForEach(s => context.CoverTypes.Add(s));
            context.SaveChanges();

            var albumStatuses = new List<AlbumStatus>
            {
                new AlbumStatus { Title = "ממתין לתמונות" },
                new AlbumStatus { Title = "ממתין לעיצוב" },
                new AlbumStatus { Title = "בעריכה" },
                new AlbumStatus { Title = "מאושר מרכזת" },
                new AlbumStatus { Title = "מאושר דפוס" },
                new AlbumStatus { Title = "הושלם" }
            };

            albumStatuses.ForEach(s => context.AlbumStatuses.Add(s));
            context.SaveChanges();

            var albumSectionStatuses = new List<AlbumSectionStatus>
            {
                new AlbumSectionStatus { Title = "לא מאושר" },
                new AlbumSectionStatus { Title = "מאושר תלמיד" },
                new AlbumSectionStatus { Title = "מאושר מרכזת" },
                new AlbumSectionStatus { Title = "מאושר דפוס" }
            };

            albumSectionStatuses.ForEach(s => context.AlbumSectionStatuses.Add(s));
            context.SaveChanges();

            var albumLockStatus = new List<AlbumLockStatus>
            {
                new AlbumLockStatus { Title = "נעול" },
                new AlbumLockStatus { Title = "פתוח לעריכה" }
            };

            albumLockStatus.ForEach(s => context.AlbumLockStatuses.Add(s));
            context.SaveChanges();

            var albumSectionLockStatus = new List<AlbumSectionLockStatus>
            {
                new AlbumSectionLockStatus { Title = "נעול דפוס" },
                new AlbumSectionLockStatus { Title = "נעול מרכזת" },
                new AlbumSectionLockStatus { Title = "פתוח לעריכה" }
            };

            albumSectionLockStatus.ForEach(s => context.AlbumSectionLockStatuses.Add(s));
            context.SaveChanges();

            var albumSectionTypes = new List<AlbumSectionType>
            {
                new AlbumSectionType { Title = "כריכה" },
                new AlbumSectionType { Title = "כיתה" },
                new AlbumSectionType { Title = "הנהלה" }
            };

            albumSectionTypes.ForEach(s => context.AlbumSectionTypes.Add(s));
            context.SaveChanges();

            var papers = new List<Paper>
            {
                new Paper { Title="נייר 105 גרם", ThicknessMM = 0.09f },
                new Paper { Title="נייר 128 גרם", ThicknessMM = 0.11f  },
                new Paper { Title="נייר 170 גרם", ThicknessMM = 0.15f },
                new Paper { Title="נייר לבדיקה 0.3ממ", ThicknessMM = 0.3f }
            };

            papers.ForEach(s => context.Papers.Add(s));
            context.SaveChanges();

            var albumSizes = new List<AlbumSize>
            {
                new AlbumSize { Title = "A4.Soft", CoverTypeId = 1, PageWidthMM=420, PageHeightMM=297, PageBleedMM=5, CoverBleedMM = 5, CartonThicknessMM=0, MinSpineMM=4, MaxSpineMM=18, BigSizeMM=0 },
                new AlbumSize { Title = "A4.Hard", CoverTypeId = 2, PageWidthMM=400, PageHeightMM=280, PageBleedMM=5, CoverBleedMM =15, CartonThicknessMM=3, MinSpineMM=4, MaxSpineMM=18, BigSizeMM=9 }                
            };

            albumSizes.ForEach(s => context.AlbumSizes.Add(s));
            context.SaveChanges();

            var albums = new List<Album>
            {
                new Album { Title = "בית ספר אביבים", NumOfCopies = 3, NumOfPages = 20, DueDate = DateTime.Now, AlbumStatusId = 1, AlbumLockStatusId = 1, EditorId = 1, PaperId = 4, OrderNum = 123, AlbumSizeId = 1, CreatedDate = DateTime.Now },
                new Album { Title = "בית ספר רבין", NumOfCopies = 3, NumOfPages = 20, DueDate = DateTime.Now, AlbumStatusId = 1, AlbumLockStatusId = 1, EditorId = 1, PaperId = 3, OrderNum = 456, AlbumSizeId=2, CreatedDate = DateTime.Now }
            };

            albums.ForEach(s => context.Albums.Add(s));
            context.SaveChanges();

            var albumSections = new List<AlbumSection>
            {
                new AlbumSection { Title = "כריכה", AlbumId = 1, EditorId = 1, SectionIdx = 0, AlbumSectionStatusId = 1, AlbumSectionLockStatusId = 1, AlbumSectionTypeId = (int)AlbumSectionTypeEnum.Cover, PagesCount = 1},
                new AlbumSection { Title = "כיתה י\"א", AlbumId = 1, EditorId = 1, SectionIdx = 1, AlbumSectionStatusId = 1, AlbumSectionLockStatusId = 1, AlbumSectionTypeId = 2, PagesCount = 8  },
                new AlbumSection { Title = "כיתה י\"ב", AlbumId = 1, EditorId = 1, SectionIdx = 2, AlbumSectionStatusId = 2, AlbumSectionLockStatusId = 1, AlbumSectionTypeId = 2, PagesCount = 6  },
                new AlbumSection { Title = "כיתה י", AlbumId = 1, EditorId = 1, SectionIdx = 3 , AlbumSectionStatusId = 1, AlbumSectionLockStatusId = 1, AlbumSectionTypeId = 2, PagesCount = 18 },

                new AlbumSection { Title = "כריכה", AlbumId = 2, EditorId = 1, SectionIdx = 0, AlbumSectionStatusId = 1, AlbumSectionLockStatusId = 1, AlbumSectionTypeId = (int)AlbumSectionTypeEnum.Cover, PagesCount = 1},
                new AlbumSection { Title = "כיתה י", AlbumId = 2, EditorId = 1, SectionIdx = 2 , AlbumSectionStatusId = 2, AlbumSectionLockStatusId = 1, AlbumSectionTypeId = 2, PagesCount = 8 }
            };

            albumSections.ForEach(s => context.AlbumSections.Add(s));
            context.SaveChanges();

            var albumPages = new List<AlbumPage>
            {
                // Cover
                new AlbumPage { AlbumSectionId = 1, SectionPageIdx =  0, },

                // First Section
                new AlbumPage { AlbumSectionId = 2, SectionPageIdx =  0, IsSingle = true, IsLeft = true},
                new AlbumPage { AlbumSectionId = 2, SectionPageIdx =  1, IsSingle = false},
                new AlbumPage { AlbumSectionId = 2, SectionPageIdx =  2, IsSingle = false},
                new AlbumPage { AlbumSectionId = 2, SectionPageIdx =  3, IsSingle = false},
                new AlbumPage { AlbumSectionId = 2, SectionPageIdx =  4, IsSingle = true, IsLeft = false},

                // Second Section
                new AlbumPage { AlbumSectionId = 3, SectionPageIdx =  0, IsSingle = true, IsLeft = true},
                new AlbumPage { AlbumSectionId = 3, SectionPageIdx =  1, IsSingle = false},                
                new AlbumPage { AlbumSectionId = 3, SectionPageIdx =  2, IsSingle = false},
                new AlbumPage { AlbumSectionId = 3, SectionPageIdx =  3, IsSingle = true, IsLeft = false},

                // Third Section
                new AlbumPage { AlbumSectionId = 4, SectionPageIdx =  0, IsSingle = true, IsLeft = true},
                new AlbumPage { AlbumSectionId = 4, SectionPageIdx =  1, IsSingle = false},
                new AlbumPage { AlbumSectionId = 4, SectionPageIdx =  2, IsSingle = false},
                new AlbumPage { AlbumSectionId = 4, SectionPageIdx =  3, IsSingle = false},
                new AlbumPage { AlbumSectionId = 4, SectionPageIdx =  4, IsSingle = false},
                new AlbumPage { AlbumSectionId = 4, SectionPageIdx =  5, IsSingle = false},
                new AlbumPage { AlbumSectionId = 4, SectionPageIdx =  6, IsSingle = false},
                new AlbumPage { AlbumSectionId = 4, SectionPageIdx =  7, IsSingle = false},
                new AlbumPage { AlbumSectionId = 4, SectionPageIdx =  8, IsSingle = false},
                new AlbumPage { AlbumSectionId = 4, SectionPageIdx =  9, IsSingle = true, IsLeft = false},


                // HARD COVER ALBUM
 
                // Cover
                new AlbumPage { AlbumSectionId = 5, SectionPageIdx =  0, },

                // First Section
                new AlbumPage { AlbumSectionId = 6, SectionPageIdx =  0, IsSingle = true, IsLeft = true},
                new AlbumPage { AlbumSectionId = 6, SectionPageIdx =  1, IsSingle = false},
                new AlbumPage { AlbumSectionId = 6, SectionPageIdx =  2, IsSingle = false},
                new AlbumPage { AlbumSectionId = 6, SectionPageIdx =  3, IsSingle = false},
                new AlbumPage { AlbumSectionId = 6, SectionPageIdx =  4, IsSingle = true, IsLeft = false},
            };

            albumPages.ForEach(s => context.AlbumPages.Add(s));
            context.SaveChanges();

            var images = new List<DesignImage>
            {
                // General Images
                new AlbumImage {  URL ="resources\\images\\Image005.jpg"},

                // Designer Images
                new AlbumImage {  URL ="resources\\images\\Image001.jpg", AlbumId=1, AlbumSectionId=1 },
                new AlbumImage {  URL ="resources\\images\\Image002.jpg", AlbumId=1, AlbumSectionId=2 },
                new AlbumImage {  URL ="resources\\images\\Image003.jpg", AlbumId=1, AlbumSectionId=3 },
                new AlbumImage {  URL ="resources\\images\\Image004.jpg", AlbumId=1, },
                
                new AlbumImage {  URL ="resources\\images\\Image005.jpg", AlbumId=2 },
            };

            ImageHelper.CreateThumbnails(images);
            images.ForEach(s => context.DesignImages.Add(s));
            context.SaveChanges();

            /*images = new List<DesignImage>
            {
                // Designer Images
                new BackgroundImage { ImageCategoryId = 2 , Title="רקע1", URL ="resources\\images\\Image001.jpg", IsSingle = true, IsRepeat = false },
                new BackgroundImage { ImageCategoryId = 3 ,Title="רקע2", URL ="resources\\images\\Image002.jpg", IsSingle = true, IsRepeat = false },
                new BackgroundImage { ImageCategoryId = 3 , Title="רקע3",URL ="resources\\images\\Image003.jpg", IsSingle = true, IsRepeat = false },
                new BackgroundImage { ImageCategoryId = 3 ,Title="רקע4", URL ="resources\\images\\Image004.jpg", IsSingle = true, IsRepeat = false },
                new BackgroundImage { ImageCategoryId = 3 , Title="רקע1",URL ="resources\\images\\Image005.jpg",IsSingle = true, IsRepeat = false },
                new BackgroundImage { ImageCategoryId = 4 , Title="רקע3",URL ="resources\\images\\Image003.jpg", IsSingle = true, IsRepeat = false },
                new BackgroundImage { ImageCategoryId = 4 ,Title="רקע4", URL ="resources\\images\\Image004.jpg", IsSingle = true, IsRepeat = false },
                new BackgroundImage { ImageCategoryId = 5 , Title="רקע1",URL ="resources\\images\\Image005.jpg",IsSingle = true, IsRepeat = false },

                new BackgroundImage { ImageCategoryId = 1 ,Title="רקע4", URL ="resources\\backgrounds\\Tile1.png", IsSingle = true, IsRepeat = true },
                new BackgroundImage { ImageCategoryId = 1 , Title="רקע1",URL ="resources\\backgrounds\\Tile2.jpg", IsSingle = true, IsRepeat = true },
            };

            ImageHelper.CreateThumbnails(images);
            images.ForEach(s => context.DesignImages.Add(s));
            context.SaveChanges();*/

            ImportBackgrounds(context);

            var approvalChecks = new List<ApprovalCheck>
            {
                new ApprovalCheck { Title = "האם כל התלמידים מופיעים באלבום?", IsSection = false},
                new ApprovalCheck { Title = "האם כל התמונות ברזולוציה מספקת?", IsSection = false},
                new ApprovalCheck { Title = "האם נבדקה הגהה/שגיאות כתיב?", IsSection = false},
                new ApprovalCheck { Title = "האם טקסט ותמונות לא גולשים מחוץ לאזור ההדפסה?", IsSection = false},
                new ApprovalCheck { Title = "האם כל התלמידים מופיעים?", IsSection = true},
                new ApprovalCheck { Title = "האם כל התמונות ברזולוציה מספקת?", IsSection = true},
                new ApprovalCheck { Title = "האם נבדקה הגהה/שגיאות כתיב?", IsSection = true},
                new ApprovalCheck { Title = "האם טקסט ותמונות לא גולשים מחוץ לאזור ההדפסה?", IsSection = true}
            };

            approvalChecks.ForEach(s => context.ApprovalChecks.Add(s));
            context.SaveChanges();

            var layoutCategories = new List<LayoutCategory>
            {
                new LayoutCategory { Title="General", TitleHebrew="כללי"},
                new LayoutCategory { Title="Single Page", TitleHebrew="עמוד בודד"},
                new LayoutCategory { Title="Double Page", TitleHebrew="עמוד כפול"},
                new LayoutCategory { Title="Text for each image", TitleHebrew="טקסט לכל תמונה"},
                new LayoutCategory { Title="Collage", TitleHebrew="קולאז'"}
            };

            layoutCategories.ForEach(s => context.LayoutCategories.Add(s));
            context.SaveChanges();

            var designMasks = new List<DesignMask>
            {
                new DesignMask { Title="Arrow", AlbumId=1, Weight=8, Color=0xFF0000, Corner="miter", MaskData="M 16.044 39.99 C 14.871 38.499 13.893 36.999 13.116 35.514 C 9.525 28.642 10.112 22.958 15.444 18.73 C 18.942 15.956 25.577 16.719 32.332 21.745 C 32.777 22.077 35.881 24.853 35.881 24.853 C 35.881 24.853 33.761 26.994 29.005 31.728 C 24.25 36.462 28.592 38.055 31.006 38.055 C 33.417 38.055 53.682 38.055 57.061 38.055 C 60.439 38.055 61.771 36.301 61.771 33.245 C 61.771 30.19 61.771 10.246 61.771 7.351 C 61.771 4.456 60.648 -0.156 56.199 4.401 C 51.753 8.96 50.213 10.317 50.213 10.317 C 50.213 10.317 46.084 7.338 43.066 5.501 C 36.071 1.239 21.439 -3.456 10.386 3.597 C 0.589 9.853 -3.516 22.735 3.503 34.411 C 4.449 35.985 5.915 37.668 8.207 39.478 C 10.499 41.287 20.865 46.115 16.044 39.99 Z"},
                new DesignMask { Title="Shield1", AlbumId=1, Weight=14, Color=0x00FF00, Corner="round", MaskData="M 21.446 50.261 C 21.446 50.261 20.283 51.109 32.224 41.971 C 44.165 32.834 42.88 26.508 42.88 26.508 L 42.88 4.576 C 42.88 4.576 37.437 4.576 32.23 4.576 C 27.023 4.576 21.37 0 21.37 0 C 21.37 0 14.526 4.571 10.509 4.571 C 5.898 4.571 0.015 4.567 0.015 4.567 L 0.015 26.498 C 0.015 26.498 -1.272 32.825 10.67 41.962 C 22.61 51.1 21.446 50.261 21.446 50.261 L 21.446 50.261 Z"},
                
                new DesignMask { Title="Shiled2", Weight=19, Color=0x0000FF, Corner="round", MaskData="M 45.051 14.154 C 42.345 10.074 39.975 5.732 37.456 1.535 C 37.713 1.963 31.148 4.105 30.444 4.213 C 26.956 4.746 24.34 3.188 22.609 0.305 L 22.609 0 C 22.581 0.053 22.55 0.102 22.521 0.152 C 22.494 0.107 22.468 0.063 22.441 0.016 L 22.441 0.289 C 20.711 3.174 18.095 4.73 14.606 4.197 C 13.903 4.09 7.338 1.947 7.595 1.52 C 5.076 5.719 2.706 10.059 0 14.139 C 2.694 15.945 3.85 17.984 3.452 21.277 C 2.637 28.023 -0.867 34.334 3.138 40.838 C 5.309 44.359 8.903 45.9 12.694 47.092 C 15.68 48.027 21.002 48.924 22.441 52.193 L 22.441 52.65 C 22.467 52.566 22.498 52.488 22.528 52.408 C 22.557 52.482 22.586 52.557 22.609 52.635 L 22.609 52.209 C 24.049 48.94 29.371 48.043 32.356 47.106 C 36.148 45.916 39.742 44.377 41.913 40.852 C 45.917 34.35 42.414 28.041 41.599 21.293 C 41.201 18 42.356 15.961 45.051 14.154 Z"},
                new DesignMask { Title="Star", Weight=10, Color=0xFFFF00, Corner="round", MaskData="M 52.288 18.534 C 51.585 18.534 51.002 19.041 50.879 19.708 L 34.931 17.391 L 27.69 2.721 C 28.168 2.487 28.499 2.002 28.499 1.436 C 28.499 0.643 27.856 0 27.064 0 C 26.272 0 25.629 0.643 25.629 1.436 C 25.629 1.968 25.923 2.428 26.354 2.675 L 19.092 17.391 L 2.843 19.752 C 2.716 19.09 2.135 18.589 1.435 18.589 C 0.643 18.589 0 19.231 0 20.023 C 0 20.816 0.643 21.459 1.435 21.459 C 1.841 21.459 2.204 21.29 2.466 21.021 L 14.197 32.455 L 11.445 48.5 C 11.375 48.489 11.305 48.479 11.231 48.479 C 10.44 48.479 9.796 49.121 9.796 49.913 C 9.796 50.706 10.44 51.349 11.231 51.349 C 12.024 51.349 12.666 50.706 12.666 49.913 C 12.666 49.719 12.627 49.533 12.557 49.364 L 27.011 41.765 L 41.402 49.331 C 41.353 49.477 41.32 49.629 41.32 49.791 C 41.32 50.584 41.963 51.226 42.755 51.226 C 43.548 51.226 44.19 50.584 44.19 49.791 C 44.19 48.999 43.548 48.356 42.755 48.356 C 42.687 48.356 42.622 48.367 42.557 48.376 L 39.825 32.455 L 51.447 21.127 C 51.684 21.3 51.973 21.404 52.288 21.404 C 53.08 21.404 53.723 20.762 53.723 19.969 C 53.723 19.177 53.08 18.534 52.288 18.534 Z"},
                new DesignMask { Title="Diamond", Weight=6, Color=0x00FFFF, Corner="miter", MaskData="M 36.877 0 L 9.819 0 L 0 13.814 L 23.348 41.748 L 46.696 13.814 L 36.877 0 Z"},
                new DesignMask { Title="Circle", Weight=6, Color=0x00FFFF, Corner="miter", MaskData="M52 104C23.3271 104 0 80.6729 0 52 0 23.3271 23.3271 0 52 0 80.6729 0 104 23.3271 104 52 104 80.6729 80.6729 104 52 104Z"},
                new DesignMask { Title="Rectangle", Weight=6, Color=0x00FFFF, Corner="miter", MaskData="M0 0 35 0 35 35 0 35Z"},
            };

            designMasks.ForEach(s => context.DesignMasks.Add(s));
            context.SaveChanges();


            // Premade Pages

            var pagesCategories = new List<PremadePageCategory>
            {
                new PremadePageCategory { Title = "Default", TitleHebrew = "כללי", Deletable = false },
                new PremadePageCategory { Title = "Management Pages", TitleHebrew = "עמודי מנהלה" },
                new PremadePageCategory { Title = "Class Pages", TitleHebrew = "עמודי כיתה" },
            };

            pagesCategories.ForEach(s => context.PremadePageCategories.Add(s));
            context.SaveChanges();

            var premadPages = new List<PremadePage>
            {
                // Doubles
                new PremadePage { Title = "Page1", IsSingle = false, AlbumSizeId = 1, PremadePageCategoryId = 1 },
                
                // Singles
                new PremadePage { Title = "Page4", IsSingle = true, AlbumSizeId = 1, PremadePageCategoryId = 1 },
            };

            premadPages.ForEach(s => context.PremadePages.Add(s));
            context.SaveChanges();

            var tips = new List<Tip>
            {
                new Tip { TipText = "באמצעות בחירת מסכה מלשונית 'מסכות', תוכל להוסיף מסגרת לתמונה נבחרת באלבום."  },
                new Tip { TipText = "לחץ על עמוד וגרור אותו כדי לשנות את סדר העמודים." }
            };

            tips.ForEach(s => context.Tips.Add(s));
            context.SaveChanges();


            var albumPrintVersionStatuses = new List<AlbumPrintVersionStatus>
            {
                new AlbumPrintVersionStatus { Title="לא מאושר" },
                new AlbumPrintVersionStatus { Title="מאושר מרכזת" },
                new AlbumPrintVersionStatus { Title="מאושר דפוס" }
            };

            albumPrintVersionStatuses.ForEach(s => context.AlbumPrintVersionStatuses.Add(s));
            context.SaveChanges();


            var renderVariables = new List<RenderVariable>
            {
                new RenderVariable { LastCheck = DateTime.Now, Quality = 90 }
            };

            renderVariables.ForEach(s => context.RenderVariables.Add(s));
            context.SaveChanges();
        }

        private void ImportBackgrounds(AlbumContext context)
        {
            var directory = HttpContext.Current.Server.MapPath("~/resources/backgrounds/A4");
            if (Directory.Exists(directory))
            {
                var files = Directory.GetFiles(directory);
                var destinationDirectory = HttpContext.Current.Server.MapPath("~/resources/backgrounds");
                var url = "resources\\backgrounds\\";

                //loop through the files
                foreach (var file in files)
                {
                    //generate file name for this image (guid string)
                    var fileName = Guid.NewGuid().ToString().Substring(0, 6) + Path.GetExtension(file);

                    var background = new BackgroundImage
                    {
                        ImageCategoryId = 1,
                        Title = Path.GetFileNameWithoutExtension(file),
                        URL = url + fileName,
                        IsSingle = true,
                        IsRepeat = false
                    };

                    //background.ThumbnailUrl = ImageHelper.GenerateThumbnailFileName(background.URL);
                    //background.LowResUrl = ImageHelper.GenerateLowResFileName(background.URL);
                    context.BackgroundImages.Add(background);

                    //if this file does not exist already
                    if (!File.Exists(destinationDirectory + "\\" + fileName))
                    {
                        try
                        {
                            File.Copy(file, destinationDirectory + "\\" + fileName);

                            ImageHelper.CreateThumbnailAndLowRes(background);
                        }
                        catch
                        {
                        }
                    }
                }
            }

            context.SaveChanges();
        }
    }
}